<%@page import="org.firestorm.traficforecast.utils.CommonUtils"%>
<%@page import="org.firestorm.traficforecast.database.NodeDAO"%>
<%@page import="java.util.HashMap"%>
<%@page import="org.firestorm.traficforecast.database.HedgeDAO"%>
<%@page import="java.io.FileOutputStream"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="java.io.File"%>
<%
	String requestContext = request.getContextPath();
	String id = request.getParameter("id");
	if (id == null || "".equals(id)) {
		response.sendRedirect(requestContext
				+ "/admin/index.jsp?message=id_null");
	} else if ("hedge".equals(id)) {

		File file;
		ServletContext context = pageContext.getServletContext();
		String contentType = request.getContentType();

		if ((contentType.indexOf("multipart/form-data") >= 0)) {
			DiskFileItemFactory factory = new DiskFileItemFactory();
			factory.setRepository(new File("c:\\tempUpload"));

			ServletFileUpload upload = new ServletFileUpload(factory);

			try {
				HashMap<String, List<FileItem>> map = new HashMap<String, List<FileItem>>();
				map.put("items", upload.parseRequest(request));
				List<FileItem> fileItems = map.get("items");
				Iterator<FileItem> i = fileItems.iterator();

				while (i.hasNext()) {
					FileItem fi = (FileItem) i.next();
					if (!fi.isFormField()) {
						String fileName = fi.getName();

						if (fileName.contains(".txt")) {
							String filePath = CommonUtils.createDir() + "\\";
							byte[] data = fi.get();
							if (fileName.lastIndexOf("\\") >= 0) {
								filePath += fileName.substring(fileName
										.lastIndexOf("\\"));
								file = new File(filePath);
							} else {
								filePath += fileName.substring(fileName
										.lastIndexOf("\\") + 1);
								file = new File(filePath);
							}
							out.print(filePath);
							fi.write(file);
							HedgeDAO hedgeD = new HedgeDAO();
							hedgeD.updateData(file);
							response.sendRedirect(requestContext
									+ "/admin/index.jsp?message=hedge_Ok");
						} else {
							response.sendRedirect(requestContext
									+ "/admin/index.jsp?message=hedge_filenamewrong");
						}
					}
				}
			} catch (Exception ex) {
				System.out.println(ex);
				response.sendRedirect(requestContext
						+ "/admin/index.jsp?message=hedge_failed");
			}
		} else {
			response.sendRedirect(requestContext
					+ "/admin/index.jsp?message=hedge_failed");
		}
	} else if ("map".equals(id)) {

		File file;
		ServletContext context = pageContext.getServletContext();
		String contentType = request.getContentType();

		if ((contentType.indexOf("multipart/form-data") >= 0)) {
			DiskFileItemFactory factory = new DiskFileItemFactory();
			factory.setRepository(new File("c:\\tempUpload"));

			ServletFileUpload upload = new ServletFileUpload(factory);

			try {
				HashMap<String, List<FileItem>> map = new HashMap<String, List<FileItem>>();
				map.put("items", upload.parseRequest(request));
				List<FileItem> fileItems = map.get("items");
				Iterator<FileItem> i = fileItems.iterator();

				while (i.hasNext()) {
					FileItem fi = (FileItem) i.next();
					if (!fi.isFormField()) {
						String fileName = fi.getName();

						if (fileName.contains(".osm")) {
							String filePath = CommonUtils.createDir() + "\\";
							byte[] data = fi.get();
							if (fileName.lastIndexOf("\\") >= 0) {
								filePath += fileName.substring(fileName
										.lastIndexOf("\\"));
								file = new File(filePath);
							} else {
								filePath += fileName.substring(fileName
										.lastIndexOf("\\") + 1);
								file = new File(filePath);
							}
							out.print(filePath);
							fi.write(file);
// 							HedgeDAO hedgeD = new HedgeDAO();
// 							hedgeD.updateData(file);
// 							NodeDAO.readData(file);
							response.sendRedirect(requestContext
									+ "/admin/index.jsp?message=map_Ok");
						} else {
							response.sendRedirect(requestContext
									+ "/admin/index.jsp?message=map_filenamewrong");
						}
					}
				}
			} catch (Exception ex) {
				System.out.println(ex);
				response.sendRedirect(requestContext
						+ "/admin/index.jsp?message=map_failed");
			}
		} else {
			response.sendRedirect(requestContext
					+ "/admin/index.jsp?message=map_failed");
		}
	}
%>